package com.ceo.auth.jwt.handler;


import com.ceo.auth.jwt.JwtEntity;
import com.ceo.auth.jwt.chain.CallableChain;
import com.ceo.auth.jwt.wrapper.JwtWrapper;
import com.ceo.common.threadlocal.EnvironCons;
import com.ceo.common.threadlocal.GlobalMemcache;
import com.ceo.common.threadlocal.ThreadEnvUtil;
import com.ceo.common.vo.UserVo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.function.Supplier;

/**
 * @Create by hefy
 * @Date 2022/1/13 11:06
 * 用户访问动作处理
 */
@Component
public class JwtUserActionHandler extends AbstractJwtHandler<JwtWrapper> {


    @Override
    public Boolean doHandle(CallableChain<Boolean, JwtWrapper> chain, Supplier<JwtWrapper> supplier) throws Exception {

        JwtEntity jwtEntity = supplier.get().getJwtEntity();
        //注入Token、当前访问类型
        GlobalMemcache.setL(EnvironCons.CLIENTTYPE, jwtEntity.getClient());
        //todo 用户访问记录

        //注入基本用户信息
        UserVo userVo = Jwt2UserVo(jwtEntity);

        ThreadEnvUtil.SetUserObject(userVo);

        return chain.doProceed(supplier);
    }

    private UserVo Jwt2UserVo(JwtEntity jwtEntity) {
        UserVo userVo = new UserVo();
        userVo.setId(jwtEntity.getId());
        userVo.setUserId(jwtEntity.getUserId());
        userVo.setUserName(jwtEntity.getUserName());
        return userVo;
    }
}
